package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 部门的控制层
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门管理")
public class DepartmentController {

    /**
     * 注入service对象
     */
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有
     * @return 部门列表
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有部门",notes = "可以查询所有的部门")
    public AjaxResult findAll(){
        return AjaxResult.createSuccess(departmentService.findAll());
    }
    /**
     * 查询部门树
     * @return 查询部门树
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树",notes = "查询部门树")
    public AjaxResult deptTree(){
        return AjaxResult.createSuccess(departmentService.deptTree());
    }
    /**
     * 查询所有
     * @return 部门列表
     */
    @GetMapping("/findParentDepts")
    @ApiOperation(value = "查询所有上级部门",notes = "可以查询所有的上级部门")
    public AjaxResult findParentDepts(){
        return AjaxResult.createSuccess(departmentService.findParentDepts());
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询",notes = "可以分页查询所有的部门")
    public AjaxResult findPage(@RequestBody DepartmentQuery query){
        return AjaxResult.createSuccess(departmentService.findPage(query));
    }

    /**
     * 新增或者修改,根据id区分
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改",notes = "可以根据id区分操作")
    public AjaxResult saveOrUpdate(@RequestBody @Valid Department department){

        //根据id区分,没有id就是新增
        try {
            departmentService.saveOrUpdate(department);

            return AjaxResult.createSuccess();
        }catch (RuntimeException e) {

            e.printStackTrace();
            return AjaxResult.createError("操作异常，"+e.getMessage());
        } catch (Exception e) {

            e.printStackTrace();
            return AjaxResult.createError("操作异常,联系管理员李裕");
        }
    }

    /**
     * 删除部门根据id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作",notes = "根据id删除部门")
    public AjaxResult delete(@PathVariable("id") Long id){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            departmentService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return AjaxResult.createError("操作异常,联系管理员李裕");
        }
    }

    /**
     * 删除部门根据id
     * @return
     */
    @PostMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody DepartmentQuery query){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            departmentService.batchRemove(query);

            return AjaxResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return AjaxResult.createError("操作异常,联系管理员李裕");
        }
    }

}